////////////////////////////////////////////////////////////////////////
//
// exercise4-4.cpp
//该程序为 学习opencv 126页 练习题 第4题
//
// 4. Make an application that reads and displays a video and is controlled by sliders.
// One slider will control the position within the video from start to end in 10 increments; 
// another binary slider should control pause/unpause. Label both sliders appropriately.
//
//点评：~还是有点bug，不能实时改变位置
#include <cv.h>
#include <highgui.h>
#include <stdio.h>

int g_nProgress = 0;
int g_nPause = 0;
int g_nWait = 0;
CvCapture* pCapture = NULL;
int nValue = -1;
char* szWindowName = "myplayer";

void switchCallback4(int nPos)
{
	double fPos = 0.0;
	fPos = (double)nPos / (double)10;
	if (pCapture)
	{
		cvSetCaptureProperty(pCapture, CV_CAP_PROP_POS_AVI_RATIO, fPos);
	}
}

void myPlayer()
{
	IplImage* pImage = NULL;

	do
	{
		if (g_nPause == 0)
		{
			break;
		}
		pImage = cvQueryFrame(pCapture);
		if (pImage)
		{
			cvShowImage(szWindowName, pImage);
		}
		nValue = cvWaitKey(50);
	}
	while (nValue == -1);
}

void switchCallback5(int nPos)
{
	if (nPos == 1)
	{
		myPlayer();
	}
}

int main(int argc,char* argv[])
{

	pCapture = cvCreateFileCapture("mytest.avi");
	if (pCapture)
	{
		cvNamedWindow(szWindowName);
		cvCreateTrackbar("progress", szWindowName, &g_nProgress, 10, switchCallback4);
		cvCreateTrackbar("pause", szWindowName, &g_nPause, 1, switchCallback5);		
	
		do
		{
			if (cvWaitKey(0) == 27)
			{
				break;
			}
		}
		while(true);

		cvReleaseCapture(&pCapture);
		cvDestroyWindow(szWindowName);
	}	
}
